<?php
class GrabTask extends Model {

    protected $table_name = 'vh_grab_task';

    //开始抓取任务(之前失败的任务可以重新抓)
    public function addNewTask($url, $channel, $title, $udb){
        if (empty($url) || empty($channel) || empty($udb)) return array('rs' => false, 'taskId' => 0, 'msg' => 'url,channel,udb三者都不能为空', 'code' => -1);
        $find = $this->find(array('url' => $url, 'status' => 0));
        if (! empty($find)) return array('rs' => true, 'taskId' => (int)$find['task_id'], 'msg' => '已经在抓取中,不必重复操作', 'code' => 2);
        /*if ($find && $find['status'] == 0) return array('rs' => true, 'taskId' => (int)$find['task_id'], 'msg' => '已经在抓取中。。', 'code' => 2);
        if ($find && $find['status'] == 1) return array('rs' => true, 'taskId' => (int)$find['task_id'], 'msg' => '该任务以前就抓取成功啦', 'code' => 3);*/
        $data = array(
            'url' => $url,
            'channel' => $channel,
            'title' => $title,
            'udb' => $udb,
            'vid' => 0,
            'status' => 0,//标记抓取中
            'start_time' => time(),
        );
        $lastId = $this->insert($data);
        if ($lastId !== false) {
            return array('rs' => true, 'taskId' => (int)$lastId, 'msg' => '任务创建成功', 'code' => 1);
        } else {
            return array('rs' => false, 'taskId' => 0, 'msg' => '任务创建失败', 'code' => 0);
        }
    }


    //标记抓取成功
    public function toggleSuccess($url, $vid){
        if (empty($url) || empty($vid)) return array('rs' => false, 'taskId' => 0, 'msg' => 'url,vid两者都不能为空', 'code' => -2);
        $find = $this->find(array('url' => $url, 'status' => 0));
        if (empty($find)) return array('rs' => false, 'taskId' => 0, 'msg' => '任务不存在,必须先创建', 'code' => 4);
        /*if ($find['status'] == 1) return array('rs' => false, 'taskId' => $find['task_id'], 'msg' => '该任务以前就抓取成功啦', 'code' => 3);
        if ($find['status'] == 2) return array('rs' => false, 'taskId' => $find['task_id'], 'msg' => '该任务以前就抓取失败,必须重新进行', 'code' => 5);*/
        $rs = $this->update(array('task_id' => $find['task_id']), array(
            'vid' => $vid,
            'status' => 1,
            'end_time' => time(),
        ));
        if ($rs !== false) {
            return array('rs' => true, 'taskId' => $find['task_id'], 'msg' => '已标记为成功抓取', 'code' => 6);
        } else {
            return array('rs' => false, 'taskId' => $find['task_id'], 'msg' => '标记为成功抓取时,出现异常', 'code' => 7);
        }
    }


    //标记抓取失败
    public function toggleFailed($url){
        if (empty($url)) return array('rs' => false, 'taskId' => 0, 'msg' => 'url不能为空', 'code' => -3);
        $find = $this->find(array('url' => $url, 'status' => 0));
        if (empty($find)) return array('rs' => false, 'taskId' => 0, 'msg' => '任务不存在,必须先创建', 'code' => 4);
        /*if ($find['status'] == 1) return array('rs' => false, 'taskId' => $find['task_id'], 'msg' => '该任务以前就抓取成功啦', 'code' => 3);
        if ($find['status'] == 2) return array('rs' => false, 'taskId' => $find['task_id'], 'msg' => '该任务以前就抓取失败,必须重新进行', 'code' => 5);*/
        $rs = $this->update(array('task_id' => (int)$find['task_id']), array(
            'status' => 2,
            'end_time' => time(),
        ));
        if ($rs !== false) {
            return array('rs' => true, 'taskId' => (int)$find['task_id'], 'msg' => '已标记为失败抓取', 'code' => 8);
        } else {
            return array('rs' => false, 'taskId' => (int)$find['task_id'], 'msg' => '标记为失败抓取时,出现异常', 'code' => 9);
        }
    }


    /**
     * 获取任务列表
     * @param string|int $status
     *      all -> 不限制状态
     *      0 -> 抓取中
     *      1 -> 抓取成功
     *      2 -> 抓取失败
     * @param int $p
     * @param $limit
     * @return array
     */
    public function getList($status = 'all', $p = 1, $limit = 20){
        $result = array();
        $conds = is_numeric($status) ? array('status' => $status) : array();
        $result['list'] = $this->select($conds, '', 'task_id DESC', array($p, $limit, 10)) ?: array();
        $result['pages'] = $this->page ?: array();
        return $result;
    }


}